package org.inote.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.inote.dao.CommentMapper;
import org.inote.dao.NoteMapper;
import org.inote.pojo.tablePojo.Comment;
import org.inote.pojo.voPojo.CommentVo;
import org.inote.service.CommentService;
import org.inote.service.NoteService;
import org.inote.utils.MarkdownUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service("CommentService")
public class CommentServiceImpl implements CommentService {
    @Resource
    private CommentMapper commentMapper;

    @Resource
    private NoteMapper noteMapper;
    @Override
    public boolean addComment(Comment comment) {
        boolean success = true;
        try{
            int i = commentMapper.addComment(comment);
            int a = noteMapper.updateCommentCount(comment.getNoteId());
        } catch (Exception e){
            success = false;
        }
        return success;
    }

    @Override
    public boolean deleteComment(Integer commentId,Integer noteId) {
        try{
            commentMapper.deleteComment(commentId);
            noteMapper.substractCommentCount(noteId);
        }catch (Exception e){
            return false;
        }
        return true;
    }

    @Override
    public PageInfo<CommentVo> queryNoteCommentsByPage(Integer noteId, Integer pageNum) {
        PageHelper.startPage(pageNum, 5);
        List<CommentVo> comments = commentMapper.queryNoteComments(noteId);
        PageInfo<CommentVo> commentPage = new PageInfo<>(comments);
        return commentPage;
    }

}
